;
(function ($) {
  $.fn.Tree = function (options_or_method, index) {
    if (!$(this).length) {
      return this;
    }

    if ($.isPlainObject(options_or_method)) {
      //如果是个初始化
      return this.each(function () {
        var id = $(this).attr('id');
        init(id, options_or_method);
      });
    } else {
      //如果是一个方法
      var id = $(this).attr('id');
      return do_method(id, options_or_method, index);
    }
  }

  $.fn.Tree.data = [];

  function init(id, options) {
    var _this = this;
    //默认设置
    var default_settings = {
      id: '',
      url: '',
      key: 'id',
      value: 'text',
      rows: {},
      status: false,
      default_node: null,
      select_node: 0,
      on_change: function () {},
      node_style: function () {},
    }
    $.fn.Tree.data[id] = $.extend(default_settings, options);
    init_btn(id);
    load_data(id);
  }

  //方法
  function do_method(id, method, index) {
    switch (method) {
      case 'getselect' :
        return  $.fn.Tree.data[id].select_node;
      case 'getnode' :
        return $.fn.Tree.data[id].rows[index];
      case 'reload' :
        load_table_data(id);
        break;
    }
  }

  function init_btn(id) {
    var treeobj = $("#" + id);
    var selector = '#' + id + ' > a';

    $(document).on('click', selector, function (e) {
      e.preventDefault();
      var index = $(this).index(selector);
      $(selector).removeClass('active').eq(index).addClass('active');


      var node_id = $(this).data('id');
      $.fn.Tree.data[id].select_node = node_id;
      var rows = $.fn.Tree.data[id].rows;
      var row = get_row_by_id(rows, node_id);
      $.fn.Tree.data[id].on_change(node_id, row);
    });
  }

  function get_row_by_id(rows, id) {
    var ret = '';
    $.each(rows, function (index, row) {
      if (row.id == id) {
        ret = row;
        return false;
      } else {
        if (typeof (row.son) != 'undefined') {
          ret = get_row_by_id(row.son, id);
          if (ret) {
            return false;
          }
        }
      }
    });

    return ret;
  }

  //加载表内容
  function load_data(id) {
    var opt = $.fn.Tree.data[id];
    var post_data = {
    };

    var key = opt.key;
    var value = opt.value;
    $.post($.fn.Tree.data[id].url, post_data, function (ret) {
      $.fn.Tree.data[id].status = true;
      $.fn.Tree.data[id].rows = ret.rows;

      var data = $.fn.Tree.data[id];
      var tableobj = $("#" + id);
      tableobj.empty();

      var default_html = '';

      var defalut_node = $.fn.Tree.data[id].default_node;
      if (defalut_node) {
        default_html = '<a class="list-group-item level_0" data-id="' + defalut_node[key] + '" href="#"><i class="fa fa-folder-open"></i> ' + defalut_node[value] + '</a>';
      }

      var html = default_html + make_table_html(id, ret.rows);
      tableobj.append(html);
      var select_node = $.fn.Tree.data[id].select_node;
      if (select_node == 0) {
        tableobj.find('a').eq(0).trigger('click');
      } else {
        tableobj.find('a[data-id="' + select_node + '"]').trigger('click');
      }

      //触发点击时间


    });
  }

  //制作 表数据
  function make_table_html(id, rows) {
    var html = '';
    var opt = $.fn.Tree.data[id];
    var key = opt.key;
    var value = opt.value;
    $.each(rows, function (index, row) {
      var icon = typeof (row.son) != 'undefined' ? '<i class="fa fa-chevron-down"></i>' : '<i class="fa fa-file-o"></i>';
      var cc = opt.node_style(row);
      cc = typeof (cc) != 'undefined' ? cc : '';
      var c = 'level_' + row.level + ' ' + cc;
      html += '<a class="list-group-item ' + c + '" data-id="' + row[key] + '" href="#" >' + icon + ' ' + row[value] + '</a>';
      if (typeof (row.son) != 'undefined') {
        html += make_table_html(id, row.son);
      }
    });

    return  html;
  }

})(jQuery);